package com.le.basic.StackAndQueue.DogAndCat;

import java.util.LinkedList;
import java.util.Queue;

public class DogCatQueue {
    private Queue<PetEnterQueue> cat;
    private Queue<PetEnterQueue> dog;
    private int count;

    public DogCatQueue() {
        this.cat = new LinkedList<>();
        this.dog = new LinkedList<>();
        this.count = 0;
    }

    // 1. 用户可以调用add方法将cat类或dog类的 实例放入队列中；
    public void add(Pet pet) {
        if (pet.getType().equals("cat")) {
            this.cat.add(new PetEnterQueue(pet, this.count++));
        } else if (pet.getType().equals("dog")) {
            this.dog.add(new PetEnterQueue(pet, this.count++));
        } else {
            throw new RuntimeException("error, it is not a dog or cat");
        }
    }

    // 2. 用户可以调用pollDog方法，将队列中dog类的实例按照 进队列的先后顺序依次弹出；
    public Cat pollCat() {
        if (this.cat.isEmpty()) {
            throw new RuntimeException("cat is null");
        }
        return (Cat) this.cat.poll().getPet();
    }

    // 3. 用户可以调用pollCat方法，将队列中cat类的实 例按照进队列的先后顺序依次弹出；
    public Dog pollDog() {
        if (this.dog.isEmpty()) {
            throw new RuntimeException("cat is null");
        }
        return (Dog) this.dog.poll().getPet();
    }

    // 4. 用户可以调用pollAll方法，将队列中所有的实例按照进队列 的先后顺序依次弹出；
    public Pet pollAll() {
        if (!this.cat.isEmpty() && !this.dog.isEmpty()) {
            if (this.cat.peek().getCount() < this.dog.peek().getCount()) {
                return this.cat.poll().getPet();
            } else {
                return this.dog.poll().getPet();
            }
        } else if (!this.cat.isEmpty()) {
            return this.cat.poll().getPet();
        } else if (!this.dog.isEmpty()) {
            return this.dog.poll().getPet();
        } else {
            throw new RuntimeException("err, queue is empty!");
        }
    }

    // 5. 用户可以调用isCatEmpty方法，检查队列中是否有cat类的实例。
    public boolean isCatEmpty(){
//        if (this.cat.isEmpty()){
//            return true;
//        }
//        return false;
        return this.cat.isEmpty();
    }

    // 6. 用户可以调用isDogEmpty方法，检查队列中是否有dog 类的实例；
    public boolean isDogEmpty(){
//        if (this.dog.isEmpty()){
//            return true;
//        }
//        return false;
        return this.dog.isEmpty();
    }

    // 7. 用户可以调用isEmpty方法，检查队列中是 否还有dog或cat的实例；
    public boolean isEmpty(){
//        if (this.cat.isEmpty() && this.dog.isEmpty()){
//            return true;
//        }
//        return false;
        return this.cat.isEmpty() && this.dog.isEmpty();
    }

    // test
    public static void main(String[] args) {
        DogCatQueue test = new DogCatQueue();
        Pet dog1 = new Dog();
        Pet cat1 = new Cat();
        Pet dog2 = new Dog();
        Pet cat2 = new Cat();
        Pet dog3 = new Dog();
        Pet cat3 = new Cat();

        test.add(dog1);
        test.add(cat1);
        test.add(dog2);
        test.add(cat2);
        test.add(dog3);
        test.add(cat3);

        test.add(dog1);
        test.add(cat1);
        test.add(dog2);
        test.add(cat2);
        test.add(dog3);
        test.add(cat3);

        test.add(dog1);
        test.add(cat1);
        test.add(dog2);
        test.add(cat2);
        test.add(dog3);
        test.add(cat3);
        while (!test.isDogEmpty()) {
            System.out.println(test.pollDog().getType());
        }
        while (!test.isEmpty()) {
            System.out.println(test.pollAll().getType());
        }
    }

}
